package com.gin.kotlin.gf2gamecommunity.navi

import com.gin.kotlin.gf2gamecommunity.routes.CommentListRoute
import com.gin.kotlin.gf2gamecommunity.routes.GameInfoRoute
import com.gin.kotlin.gf2gamecommunity.routes.ThemeDetailRoute
import com.gin.kotlin.gf2gamecommunity.routes.UserCenterRoute
import okhttp3.HttpUrl

/**
 * url地址与路线对象的匹配枚举
 * @param match (httpUrl: HttpUrl) -> Boolean
 * @param toRoute (httpUrl: HttpUrl) -> Any
 * @constructor
 */
enum class UrlRoute(
    val match: (httpUrl: HttpUrl) -> Boolean,
    val toRoute: (httpUrl: HttpUrl) -> Any,
) {
    // 主题详情
    CommentList(
        { it.id != null && it.lastPath == "threadInfo" },
        { CommentListRoute(it.id!!) }
    ),

    // 话题详情
    ThemeDetail(
        { it.id != null && it.lastPath == "themeInfo" },
        { ThemeDetailRoute(it.id!!) }
    ),

    // 用户中心
    UserCenter(
        { it.id != null && it.lastPath == "user" },
        { UserCenterRoute(it.id!!) }
    ),

    // 游戏信息
    GameInfo(
        { it.id != null && it.lastPath == "otherData" },
        { GameInfoRoute(it.id!!) }
    ),

    ;


    companion object {
        fun firstOrNull(httpUrl: HttpUrl) = entries.firstOrNull { it.match(httpUrl) }
    }
}


private val HttpUrl.id get() = this.queryParameter("id")?.toIntOrNull()
private val HttpUrl.lastPath get() = this.pathSegments.lastOrNull()